/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import models.Course;
import models.User;
import org.hibernate.Session;
import service.CourseDao;
import service.HibernateUtil;
import service.UserDao;

/**
 *
 * @author kjeld
 */
public class DigitalClassRoomController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        HibernateUtil HU = new HibernateUtil();
        Session sessie = HU.openSession();
        if (request.getParameter("courseID") != null) {
            //File path = new File(root + "/files/dcfiles/" + filename);
            CourseDao cd = new CourseDao(sessie);
            long d = Long.parseLong(request.getParameter("courseID"));//dit moet dynamsich worden
            Course course = cd.get(d);
            String courseName = course.getName();
            courseName = courseName.toLowerCase();
            char[] charArray = courseName.toCharArray();
            String livestreamName = "";
            for (int i = 0; i < charArray.length; i++) {
                char c = charArray[i];
                if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
                    livestreamName += c;
                }
            }
            try{
            String root = getServletContext().getRealPath("/");
            String path = root + "files/dcfiles/" + livestreamName + "/";
            session.setAttribute("path", path);
            File vDir = new File(root + "files/dcfiles/" + livestreamName);
            File[] contents = vDir.listFiles();
            File pptFile = null;
            File pdfFile = null;
            for (File theFile : contents) {
                System.out.println("Filename: " + theFile.getName());
                if (theFile.isDirectory() == false) {
                    if (theFile.getName().contains(".pdf")) {
                        pdfFile = theFile;
                    } else if (theFile.getName().contains(".ppt")) {
                        pptFile = theFile;
                    }
                }
            }
            if (pdfFile != null) {
                session.setAttribute("pdfFile", pdfFile);
            }
            if (pptFile != null) {
                session.setAttribute("pptFile", pptFile);
            }
            } catch(Exception exception){
                System.out.println("Exception: " + exception);
            }
            session.setAttribute("livestreamname", livestreamName);
            session.setAttribute("courseID", d + "");
        } else {
            long userID = 0;
            for (Cookie cookie : request.getCookies()) {
                if (cookie.getName().equals("userID")) {
                    userID = Long.parseLong(cookie.getValue());
                }
            }
            session.setAttribute("courseID", null);
            CourseDao cd = new CourseDao(sessie);
            UserDao udao = new UserDao(sessie);
            //User user = udao.get(Long.parseLong(teacherArray.nextToken()));
            //user.addCourse(course);

            User user = udao.get(userID);
            Set<Course> courses = user.getCourses();
            session.setAttribute("courses", courses);
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher("digitalclassroom.jsp");
        dispatcher.forward(request, response);

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws
            IOException, ServletException {
    }
}
